const router = require('koa-router')()
const { login } = require('../controller/user')
const { SuccessModel, loginFailedModel } = require('../model/resModel')

router.prefix('/api/user')

router.post('/login', async function (ctx, next) {
    const { username, password } = ctx.request.body
    const data = await login(username, password)
    
    if (data.uname) {
        // 设置 session
        ctx.session.username = data.uname
        
        ctx.body = new SuccessModel(data)
        return
    }
    ctx.body = new loginFailedModel('登录失败')
})


module.exports = router